package com.byd.div11.zhang_long.module1.swagger;

import net.sf.jsqlparser.expression.operators.arithmetic.Concat;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Profile;
import springfox.documentation.builders.ParameterBuilder;
import springfox.documentation.builders.RequestHandlerSelectors;
import springfox.documentation.schema.ModelRef;
import springfox.documentation.service.ApiInfo;
import springfox.documentation.service.Contact;
import springfox.documentation.service.Parameter;
import springfox.documentation.spi.DocumentationType;
import springfox.documentation.spring.web.plugins.Docket;
import springfox.documentation.swagger2.annotations.EnableSwagger2;

import java.util.ArrayList;
import java.util.List;

/**
 * @Author: Dragon
 * @Create: 2023/10/31 - 19:22
 */
@Configuration
@EnableSwagger2
@Profile({"prd","tst"})
public class Swagger {

    @Bean
    public Docket docket(){
        return new Docket(DocumentationType.SWAGGER_2)
                .apiInfo(apiInfo())
                .groupName("测试模块1-员工服务")
                .enable(true)
                .select()
                .apis(RequestHandlerSelectors.basePackage("com.byd.div11.zhang_long.module1.controller"))
                .build()
                .globalOperationParameters(this.getParameterList());
    }

    private ApiInfo apiInfo(){

        Contact contact = new Contact("byd", "http://localhost:8080/swagger-ui.html#/", "");
        return new ApiInfo(
                "微服务测试项目",
                "byd",
                "1.0",
                "http://localhost:8080/swagger-ui.html#/",
                contact,
                "Apache 2.0",
                "http://www.apache.org/licenses/LiCENSES-2.0",
                new ArrayList<>());
    }

    private List<Parameter> getParameterList() {

        List<Parameter> pars = new ArrayList<>();

        ParameterBuilder authorization = new ParameterBuilder();
        authorization.name("Authorization").description("授权token")
                .modelRef(new ModelRef("string"))
                .parameterType("header")
                .required(true).build();
        pars.add(authorization.build());

        ParameterBuilder bydHeader = new ParameterBuilder();
        bydHeader.name("x-byd-header").description("用户id参数")
                .modelRef(new ModelRef("string"))
                .parameterType("header")
                .required(true).build();
        pars.add(bydHeader.build());

        return pars;
    }
}
